import 'package:dio/dio.dart';
import 'package:dio_log/dio_log.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_app/http/api.dart';

class HttpDio {
  Dio _dio;
  static HttpDio _http;

  static HttpDio getInstance() {
    if (_http == null) {
      _http = HttpDio();
    }
    return _http;
  }

  HttpDio() {
    if (_dio == null) {
      _dio = createHttpDio();
    }
  }

  get(String uri, Map<String, dynamic> params) {
    _dio
        .get(uri, queryParameters: params)
        .then((value) => {print(value.toString())});
  }

  ///只有url的get请求
  getUri(String uri,
      {Function(dynamic json) onNext,
      Function(int code, String reason) onFail,
      Function() onComplete}) {
    debugPrint(uri);
    _dio.get(uri).then((response) {
      debugPrint(response.toString());
      if (response.statusCode == 200) {
        if (onNext != null) {
          onNext(response.data);
        }
      } else {
        if (onFail != null) {
          onFail(response.statusCode, response.statusMessage);
        }
      }
      if (onComplete != null) {
        onComplete();
      }
    });
  }

  /// 创建dio
  Dio createHttpDio() {
    var dio = Dio(BaseOptions(
        connectTimeout: 10000,
        receiveTimeout: 5000,
        sendTimeout: 5000,
        baseUrl: Api.BASE_URL_LRY));
    dio.interceptors.add(DioLogInterceptor());
    return dio;
  }

  ///GoalGo的请求头
// Map<String,dynamic> getHpHeaders() {
//   //请求头
//   return {
//     "type": "android",
//     "system": "OA",
//     "hardware": "Android",
//     "loginToken": Global().getLoginToken()
//   };
// }
}
